package com.aaa.mrcai.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 * @ Author: Lining
 * @ Date: 2021/12/19 17:07
 */
@Slf4j
@Aspect
@Component
public class LogAOP {


    private long start = 0;
    @Pointcut("execution(*  com.aaa.mrcai.controller.*.*(..))")
    public void hehehe(){

    }

    @Before("hehehe()")
    public void haha(JoinPoint point){

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();

        String addr = request.getRemoteAddr();
        String uri = request.getRequestURI();
        Object[] args = point.getArgs();


        start = System.currentTimeMillis();
        log.info("===================请求的地址是:================="+addr);
        log.info("===================请求的路径是:================="+uri);
        log.info("===================请求的参数是:================="+ Arrays.toString(args));
    }

    @After("hehehe()")
    public void hehe(){
        long end = System.currentTimeMillis();
        log.info("=================耗时================="+ (end-start));
        log.info("=================结束了=================");
    }
}
